package com.baomidou.springwind.controller;

import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.springwind.entity.Attendance;
import com.baomidou.springwind.service.IAttendanceService;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/4/14.
 */

@Controller
@RequestMapping("/attendance")
public class AttendanceController extends BaseController {
    @Autowired
    private IAttendanceService iAttendanceService;

    @RequestMapping("/list")
    @Permission("7001")
    public String getAttendancePage() {
        return "/attendance/list";
    }

    @ResponseBody
    @RequestMapping("/getAttendanceList")
    @Permission("7001")
    public String getAttendanceList() {
        Page<Attendance> page = this.getPage();
        List<Attendance> list = iAttendanceService.selectAllAttendance(page);
        page.setRecords(list);
        page.setTotal(list.size());
        return jsonPage(page);
    }


    @ResponseBody
    @Permission("7001")
    @RequestMapping("/addAttendance")
    public String addAttendance(Attendance attendance, String id) {
        boolean rtl = false;
        if (attendance != null) {
            Date date = new Date();
            attendance.setDatastatus(1);
            attendance.setEmployeeid(id);
//            attendance.setDate(DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss"));
            attendance.setCreatetime(DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss"));
            attendance.setChangetime(DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss"));
            attendance.setId("a" + DateFormatUtils.format(date, "yyyyMMddHHmmss"));
            rtl = iAttendanceService.insert(attendance);
        }
        return callbackSuccess(rtl);
    }


    @ResponseBody
    @RequestMapping("/deleteAttendance/{id}")
    @Permission("7001")
    public String delAttendance(@PathVariable String id) {
        iAttendanceService.deleteById(id);
        return Boolean.TRUE.toString();
    }

    @RequestMapping("/editAttendance")
    @Permission("7001")
    public String edit(Model model, String id) {
        model.addAttribute("attendance", iAttendanceService.selectById(id));
        return "/attendance/edit";
    }

    @ResponseBody
    @RequestMapping("/updateAttendance")
    @Permission("7001")
    public String updata(Attendance attendance) {
        boolean rtl = false;
        if (attendance != null) {
            attendance.setChangetime(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            rtl = iAttendanceService.updateById(attendance);
        }
        return callbackSuccess(rtl);
    }


    @ResponseBody
    @RequestMapping("/seach/{key}_{keyword}")
    @Permission("7001")
    public String seach(@PathVariable("key") String key, @PathVariable("keyword") String keyword) {
        Page<Attendance> page = this.getPage();
        try {
            keyword = URLDecoder.decode(keyword, "UTF-8");

            if (key.equals("employeename")) {
                page.setRecords(iAttendanceService.selectByEmployeeName(page.getOffset(), page.getLimit(), keyword));
            } else if (key.equals("employeenum")) {
                page.setRecords(iAttendanceService.selectByEmployeeNum(page.getOffset(), page.getLimit(), keyword));
            } else if (key.equals("employeeid")) {
                page.setRecords(iAttendanceService.selectByEmployeeId(page.getOffset(), page.getLimit(), keyword));
            } else if (key.equals("departmentname")) {
                page.setRecords(iAttendanceService.selectByDepartmentName(page.getOffset(), page.getLimit(), keyword));
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return jsonPage(page);
    }

}
